package com.luojialong.onlineJudge.judge.codesandbox.impl;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;

import com.luojialong.onlineJudge.judge.codesandbox.CodeSandBox;
import com.luojialong.onlineJudge.judge.codesandbox.model.ExecuteCodeRequest;
import com.luojialong.onlineJudge.judge.codesandbox.model.ExecuteCodeResponse;
import com.luojialong.onlineJudge.judge.codesandbox.model.JudgeInfo;
import com.luojialong.onlineJudge.model.enums.JudgeInfoMessageEnum;
import org.apache.commons.lang3.StringUtils;


import java.util.ArrayList;
import java.util.Arrays;

public class RemoteCodeSandbox implements CodeSandBox {

    private static final String AUTH_REQUEST_HEADER = "auth";

    private static final String AUTH_REQUSET_SECRET = "secretKey";


    public static void main(String[] args) {
        RemoteCodeSandbox remoteCodeSandbox = new RemoteCodeSandbox();
        String code = "import java.util.Scanner;\n" +
                "\n" +
                "public class Main{\n" +
                "\n" +
                "    public static void main(String[] args) {\n" +
                "        Scanner scanner = new Scanner(System.in);\n" +
                "        int a = scanner.nextInt();\n" +
                "        int c = scanner.nextInt();\n" +
                "        Integer b = null;\n" +
                "        if (a == 100) {\n" +
                "            b = 7;\n" +
                "        }\n" +
                "        System.out.print(a + b + c);\n" +
                "    }\n" +
                "}";
        ExecuteCodeRequest executeCodeRequest = new ExecuteCodeRequest();
        executeCodeRequest.setCode(code);
        executeCodeRequest.setLanguage("java");
        executeCodeRequest.setInputList(Arrays.asList("12 34"));
        ExecuteCodeResponse executeCodeResponse = remoteCodeSandbox.executeCode(executeCodeRequest);
        System.out.println(executeCodeResponse);
    }

    @Override
    public ExecuteCodeResponse executeCode(ExecuteCodeRequest executeCodeRequest) {
        System.out.println("这是远程接口调用");
        String url = "http://118.24.87.165:8091/executeCode";
        String json = JSONUtil.toJsonStr(executeCodeRequest);
        String responseStr = HttpUtil.createPost(url)
                .header(AUTH_REQUEST_HEADER, AUTH_REQUSET_SECRET)
                .body(json)
                .execute()
                .body();
        if (StringUtils.isBlank(responseStr)) {
            ExecuteCodeResponse executeCodeResponse = new ExecuteCodeResponse();
            executeCodeResponse.setMessage(JudgeInfoMessageEnum.PARAM_ERROR.getValue());
            JudgeInfo judgeInfo = new JudgeInfo();
            judgeInfo.setTime(-1L);
            judgeInfo.setMemory(-1L);
            executeCodeResponse.setJudgeInfo(judgeInfo);
            executeCodeResponse.setOuputList(new ArrayList<>());
            return executeCodeResponse;
        }
        System.out.println(JSONUtil.toBean(responseStr, ExecuteCodeResponse.class));
        return JSONUtil.toBean(responseStr, ExecuteCodeResponse.class);
    }
}
